package cn.gdcp.sqlitetest;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button createDatabaseBtn;
    Button upGradeDatabaseBtn;
    Button upGradeDataBtn;
    Button addDataBtn;
      Button deleteDataBtn;
      Button queryDataBtn;
    MyDatabaseHelper myDatabaseHelper;
    Button nextActivityBtn;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initDatabase();
        initButtonView();
        initOnclickButton();
    }

    private void initDatabase() {
        myDatabaseHelper=new MyDatabaseHelper(this,"BookStore",null,2);

    }

    private void initOnclickButton() {
        createDatabaseBtn.setOnClickListener(this);
        upGradeDatabaseBtn.setOnClickListener(this);
        addDataBtn.setOnClickListener(this);
        upGradeDataBtn.setOnClickListener(this);
        deleteDataBtn.setOnClickListener(this);
       queryDataBtn.setOnClickListener(this);
       nextActivityBtn.setOnClickListener(this);
    }

    private void initButtonView() {
          createDatabaseBtn=findViewById(R.id.create_database);
          upGradeDatabaseBtn=findViewById(R.id.upgrade_database);
          addDataBtn=findViewById(R.id.add_data);
          upGradeDataBtn=findViewById(R.id.upgrade_data);
          deleteDataBtn=findViewById(R.id.delete_data);
          queryDataBtn=findViewById(R.id.query_data);
          nextActivityBtn=findViewById(R.id.next_activity);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.create_database:
                createDatabase();
                break;
            case R.id.upgrade_database:
                upGradeDatabase();
                break;
            case R.id.add_data:
                addData();
                break;
            case R.id.upgrade_data:
                upGradeData();
                break;
            case R.id.delete_data:
                deleteData();
                break;
            case R.id.query_data:
                queryData();
             break;
            case  R.id.next_activity:
                startNextActivity();
        }
    }

    private void startNextActivity() {
        Intent intent=new Intent(this,SecondActivity.class);
            startActivity(intent);


    }

    private void queryData() {
        SQLiteDatabase database=myDatabaseHelper.getReadableDatabase();
        Cursor cursor=database.query("Book",null,null,null,null,null,null);
            if (cursor.moveToFirst()){
                do {
                    String name=cursor.getString(cursor.getColumnIndex("name"));
                    String id=String.valueOf(cursor.getInt(cursor.getColumnIndex("id")));
                    String height=String.valueOf(cursor.getInt(cursor.getColumnIndex("height")));
                    Log.e("name",name);
                    Log.e("id",id);
                    Log.e("height",height);
                }while (cursor.moveToNext());
                }
            cursor.close();
        Toast.makeText(MainActivity.this,"查询数据成功",Toast.LENGTH_SHORT).show();
    }

    private void deleteData() {
          SQLiteDatabase database=myDatabaseHelper.getReadableDatabase();
           database.delete("Book","height=?",new String[]{"175"});
        Toast.makeText(MainActivity.this,"删除数据成功",Toast.LENGTH_SHORT).show();
    }

    private void upGradeData() {
        SQLiteDatabase database=myDatabaseHelper.getReadableDatabase();
          ContentValues values=new ContentValues();
           values.put("height",175);
         database.update("Book",values,"name=?",new String[]{"陆游"});
        Toast.makeText(MainActivity.this,"修改数据成功",Toast.LENGTH_SHORT).show();

    }

    private void addData() {
        SQLiteDatabase database=myDatabaseHelper.getReadableDatabase();
        ContentValues values=new ContentValues();
        values.put("name","陆秀夫");
        values.put("height",171.1);
        database.insert("Book",null,values);
        values.clear();
        values.put("name","陆游");
        values.put("height",173.1);
        database.insert("Book",null,values);
        Toast.makeText(MainActivity.this,"插入数据成功",Toast.LENGTH_SHORT).show();

    }

    private void upGradeDatabase() {
        myDatabaseHelper.getReadableDatabase();
    }

    private void createDatabase() {
               myDatabaseHelper.getReadableDatabase();
    }
}
